package com.newmuis.open.codeeval.easy;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * CodeEval.com > Easy Challenges > Set Intersection
 * 
 * Description:
 * 
 * You are given two sorted list of numbers(ascending order). The lists themselves are comma
 * delimited and the two lists are semicolon delimited. Print out the intersection of these two
 * sets.
 * 
 * Input sample:
 * 
 * File containing two lists of ascending order sorted integers, comma delimited, one per line. e.g.
 * 
 * 1,2,3,4;4,5,6
 * 7,8,9;8,9,10,11,12
 * 
 * Output sample:
 * 
 * Print out the ascending order sorted intersection of the two lists, one per line
 * e.g.
 * 
 * 4
 * 8,9
 * 
 * @author Jonathan Newmuis (jon@newmuis.com)
 * @category easy
 * 
 */
public class intersection {
	/**
	 * @param args should contain only one CLI argument: the path to an input file which specifies
	 *            the input in the defined format.
	 * @throws IOException if there is a problem reading the given input file.
	 */
	public static void main(final String[] args) throws IOException {
		// Verify input argument length.
		if (args.length < 1) {
			throw new RuntimeException("Must specify input file path.");
		} else if (args.length > 1) {
			throw new RuntimeException("Illegal input arguments specified.");
		}

		// Read file from specified path.
		final BufferedReader reader = new BufferedReader(new FileReader(new File(args[0])));
		String line;

		while ((line = reader.readLine()) != null) {
			// Parse the line into two lists.
			final String[] lists = line.split(";");

			// Verify the length of the "intersection" arguments given in the input file.
			if (lists.length != 2) {
				throw new RuntimeException("Input file must contain two lists per line.");
			}

			// Get the elements of each list.
			final String[] listA = lists[0].split(",");
			final String[] listB = lists[1].split(",");
			final StringBuilder listBuilder = new StringBuilder();

			// Find the intersection by stepping through the lists simultaneously.
			for (int indexA = 0, indexB = 0; indexA < listA.length && indexB < listB.length;) {
				try {
					final int valueA = Integer.parseInt(listA[indexA]);
					final int valueB = Integer.parseInt(listB[indexB]);

					if (valueA < valueB) {
						indexA++;
					} else if (valueB < valueA) {
						indexB++;
					} else {
						// valueA == valueB, thus both are in intersection(listA, listB).
						listBuilder.append(valueA);
						listBuilder.append(",");
						indexA++;
						indexB++;
					}
				} catch (final NumberFormatException e) {
					throw new RuntimeException("Input file contained non-numeric arguments.");
				}
			}

			if (listBuilder.indexOf(",") > 0) {
				// Oops... left an extra comma at the end, there.
				listBuilder.deleteCharAt(listBuilder.length() - 1);
			}

			// Output the contents of the buffer.
			System.out.println(listBuilder.toString());
		}
	}
}
